﻿Imports System.Data
Imports System.Data.SqlClient
'Hacemos referencia al proyecto BusinessEntities
Imports BE = BusinessEntities

Public Class ProveedorDAO
    Dim conexion As SqlConnection
    Dim comando As SqlDataAdapter
    Public Shared errores As String
    Dim com As SqlCommand

    'Instanciamos la clase Conexion
    Dim cn As New Conexion


    Public Function ListarProveedores() As DataSet
        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_LISTARPROVEEDORES", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Dim ds As New DataSet
            comando.Fill(ds, "LosProveedores")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function



    Public Function ServiciosXProveedores() As DataSet
        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_SERVICIOSXPROVEEDOR", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Dim ds As New DataSet
            comando.Fill(ds, "ServiciosXProveedores")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function BuscarProveedor(ByVal objeto As BE.ProveedorBE) As DataSet
        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_BUSCARPROVEEDOR", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Definimos el paràmetro del stored procedure
            comando.SelectCommand.Parameters.Add _
            (New SqlParameter("@CODIGO", SqlDbType.Char, 3))

            comando.SelectCommand.Parameters("@CODIGO").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@CODIGO").Value = objeto.codigo


            'Dim ds As New DataSet
            comando.Fill(ds, "ElProveedor")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function InsertarProveedor(ByVal objeto As BE.ProveedorBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_INSERTARPROVEEDOR", conexion)
            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros de entrada

            com.Parameters.Add(New SqlParameter("@CODIGO", SqlDbType.Char, 3))
            com.Parameters("@CODIGO").Direction = ParameterDirection.Input
            com.Parameters("@CODIGO").Value = objeto.codigo

            com.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 30))
            com.Parameters("@NOMBRE").Direction = ParameterDirection.Input
            com.Parameters("@NOMBRE").Value = objeto.razonsocial


            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally

            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function


    Public Function ActualizarProveedor(ByVal objeto As BE.ProveedorBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_ACTUALIZARPROVEEDOR", conexion)
            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros de entrada

            com.Parameters.Add(New SqlParameter("@CODIGO", SqlDbType.Char, 3))
            com.Parameters("@CODIGO").Direction = ParameterDirection.Input
            com.Parameters("@CODIGO").Value = objeto.codigo

            com.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 30))
            com.Parameters("@NOMBRE").Direction = ParameterDirection.Input
            com.Parameters("@NOMBRE").Value = objeto.razonsocial

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally

            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function

    Public Function EliminarProveedor(ByVal objeto As BE.ProveedorBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_ELIMINARPROVEEDOR", conexion)

            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros de entrada

            com.Parameters.Add(New SqlParameter("@CODIGO", SqlDbType.Char, 3))
            com.Parameters("@CODIGO").Direction = ParameterDirection.Input
            com.Parameters("@CODIGO").Value = objeto.codigo

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally

            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function



    Public Function ChapaError() As String
        Return errores
    End Function
End Class
